package com.example.spider;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;

public class paramDemo {
    public static void main(String[] args) throws Exception {
        //模拟浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //构建带参数的请求
        URIBuilder uriBuilder = new URIBuilder("https://soso.nipic.com/");
        uriBuilder.addParameter("q","面");
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        System.err.println("发起的请求："+ httpGet);
        //发送请求，获得结果
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //解析数据
        if (response.getCode() == 200){
            HttpEntity httpEntity = response.getEntity();
            String res = EntityUtils.toString(httpEntity, "utf8");
            System.out.println(res);
        }else {
            System.err.println("解析失败！");
        }
    }
}
